﻿using TCC.Framework;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using System.Reflection;

namespace TCC.Framework
{
    [CustomEditor(typeof(UIBehaviour))]
    public class UIBehaviourInspector : Editor
    {
        private GUIContent copyContent;
        private GUIContent refreshContent;
        SerializedProperty _id;
        SerializedProperty _enableAnim;
        SerializedProperty _uiAnim;
        List<string> uiAnims = new List<string>();

        private int SelecteIndex
        {
            get => uiAnims.IndexOf(_uiAnim.stringValue);
            set
            {
                if (uiAnims.IndexOf(_uiAnim.stringValue) == value) return;
                serializedObject.Update();
                _uiAnim.stringValue = uiAnims[value];
                serializedObject.ApplyModifiedProperties();
            }
        }

        void OnEnable()
        {
            copyContent = new GUIContent();
            copyContent.image = EditorGUIUtility.IconContent("d_TreeEditor.Duplicate").image;
            copyContent.tooltip = "复制";

            refreshContent = new GUIContent();
            refreshContent.image = EditorGUIUtility.IconContent("RotateTool").image;
            refreshContent.tooltip = "重置ID";

            _enableAnim = serializedObject.FindProperty("enableAnim");
            _uiAnim = serializedObject.FindProperty("uiAnimType");
            uiAnims.Clear();
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
                foreach (var type in assembly.GetTypes())
                    if (typeof(UIAnimBase).IsAssignableFrom(type))//判断type是否是其派生类
                        if (type.IsClass && !type.IsAbstract && type.Name != "UIAnimBase")
                            uiAnims.Add(type.Name);
            if (string.IsNullOrEmpty(_uiAnim.stringValue))
                SelecteIndex = 0;

            _id = serializedObject.FindProperty("ID");
            if (string.IsNullOrEmpty(_id.stringValue))
                _id.stringValue = Selection.activeGameObject.name + "_" + long.Parse(DateTime.Now.ToString("yyyyMMddhhmmss")).ToString("X");

            serializedObject.ApplyModifiedProperties();
        }
        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            GUILayout.BeginHorizontal();
            GUILayout.Label("ID", GUILayout.Width(20));
            GUI.enabled = false;
            EditorGUILayout.PropertyField(_id, new GUIContent());
            GUI.enabled = true;
            if (GUILayout.Button(copyContent, "InvisibleButton", GUILayout.Width(20), GUILayout.Height(20)))
            {
                GUIUtility.systemCopyBuffer = (target as UIBehaviour).ID;
            }
            if (GUILayout.Button(refreshContent, "InvisibleButton", GUILayout.Width(20), GUILayout.Height(20)))
            {
                _id.stringValue = Selection.activeGameObject.name + "_" + long.Parse(DateTime.Now.ToString("yyyyMMddhhmmss")).ToString("X");
            }
            GUILayout.EndHorizontal();

            EditorGUILayout.PropertyField(_enableAnim, new GUIContent("开启动画"));
            if (_enableAnim.boolValue)
            {
                SelecteIndex = EditorGUILayout.Popup(new GUIContent("动画"), SelecteIndex, uiAnims.ToArray());
            }
            serializedObject.ApplyModifiedProperties();
        }
    }
}